#include "PaintUtils.h"

#include "ClientSettings.h"
#include "TileState.h"


// Implementation 
PaintUtils* PaintUtils::_instance = 0;
HBRUSH PaintUtils::mActiveTileBrush = NULL;
HBRUSH PaintUtils::mInactiveTileBrush = NULL;
HBRUSH PaintUtils::mEmptyTileBrush = NULL;


PaintUtils* PaintUtils::inst() {
	if (_instance == 0) {
		_instance = new PaintUtils;
	}
	return _instance;
}

PaintUtils::PaintUtils()
{

	// Cria os brushs
	mActiveTileBrush = CreateSolidBrush(ClientSettings::TILE_ACTIVE_COLOR);
	mInactiveTileBrush = CreateSolidBrush(ClientSettings::TILE_INACTIVE_COLOR);
	mEmptyTileBrush  = CreateSolidBrush(ClientSettings::TILE_EMPTY_COLOR);
}

PaintUtils::~PaintUtils()
{
	// Liberta os brushs
	DeleteObject(mActiveTileBrush);
	DeleteObject(mInactiveTileBrush);
}

HGDIOBJ PaintUtils::TileBrushFromState( TileState& _state )
{
	if( _state.Has(TileState::INACTIVE))
		return InactiveTileBrush();

	if( _state.Has(TileState::EMPTY) && !_state.Has(TileState::HOVERED))
		return EmptyTileBrush();

	if( _state.Has(TileState::ACTIVE | TileState::HOVERED))
		return ActiveTileBrush();



	return NULL;
}

COLORREF PaintUtils::TileFontColorFromState( TileState& _state )
{
	if( _state.Has(TileState::INACTIVE | TileState::HOVERED | TileState::DRAGGED))
		return ClientSettings::LETTERS_INACTIVE_COLOR;

	return ClientSettings::LETTERS_ACTIVE_COLOR;	
}


void PaintUtils::DrawBagTile( HDC _surface, RECT& _rect, TileState& _state )
{
	HGDIOBJ tmpSelectObject = SelectObject(_surface, PaintUtils::inst()->TileBrushFromState(_state));
	RoundRect( _surface, _rect.left, _rect.top, _rect.right, _rect.bottom, 15, 15 );
	SelectObject(_surface, tmpSelectObject);
}

void PaintUtils::DrawTileLetter( HDC _surface, RECT& _rect, char _letter, TileState& _state )
{
	int bkMode = SetBkMode(_surface, TRANSPARENT);

	COLORREF tmpColor = SetTextColor(_surface, TileFontColorFromState(_state));
	DrawText(_surface, (LPCTSTR)&_letter, -1, &_rect, DT_SINGLELINE | DT_CENTER | DT_VCENTER);

	SetTextColor(_surface, tmpColor);
	SetBkMode(_surface, bkMode);
}

void PaintUtils::DrawBoardTile( HDC _surface, RECT& _rect, TileState& _state )
{
	HGDIOBJ tmpSelectObject = SelectObject(_surface, PaintUtils::inst()->TileBrushFromState(_state));
	Rectangle( _surface, _rect.left, _rect.top, _rect.right, _rect.bottom );
	SelectObject(_surface, tmpSelectObject);
}


